<template>
	<view class="content">
		<view class="top-box">
			<view class="item-top">
				<view class="input-search">
					<input type="text" placeholder="搜索场地名称" v-model="keywords" />
					<image src="../../static/images/home/切片@2x.png" @click="getGroundMap('move')"></image>
				</view>
				<view class="select-box">
					<uni-data-select v-model="selected" :localdata="selectList" @change="onChange"></uni-data-select>
				</view>
			</view>
			<view class="item-bottom">
				<view class="item-info">
					<image src="../../static/images/maintenance/device.png" class="image"></image>
					<view>总设备: </view>
					<text class="value">{{total_devices}}</text>
				</view>
				<view class="item-info">
					<image src="../../static/images/maintenance/device.png" class="image"></image>
					<view>正常设备:</view>
					<text class="value">{{total_health_devices}}</text>
				</view>
			</view>
		</view>
		<view class="ground-list" @click="navigatorPage">
			<image src="../../static/images/maintenance/list.png" class="image"></image>
			<view>场所列表</view>
		</view>
		<view class="ground-info" v-if="selectGround&&showGround" @click="showGround =false">
			<view class="item-top">
				<view class="ground-name">{{selectGround.name}}</view>
				<view class="distance" @click.stop="navigatorMap">
					<image src="../../static/images/maintenance/map.png" class="image"></image>
					<view>{{selectGround.distance}}km</view>
				</view>
			</view>
			<view class="item-center">{{selectGround.city}} {{selectGround.address}}</view>
			<view class="item-bottom">
				<view class="phone blue" @click.stop="makePhoneCall(selectGround.kefu)">
					<image src="../../static/images/maintenance/phone.png" class="image"></image>
					{{selectGround.kefu}}
				</view>
				<view class="device">
					<image src="../../static/images/maintenance/equip.png" class="image"></image>
					<view> 正常: <text class="blue">{{selectGround.health_devices}}</text>/{{selectGround.total_devices}}
					</view>
				</view>
			</view>
		</view>
		<map id="map" class="map" :show-location="true" scale="16" :markers="groundMapList" @markertap='getDetail'
			:latitude="latitude" :longitude="longitude"></map>

	</view>
</template>

<script>
	import api from '@/api/index.js';
	export default {
		data() {
			return {
				keywords:'',
				latitude: '',
				longitude: '',
				myLatitude: '',
				myLongitude: '',
				groundMapList: [],
				selectGround: null,
				showGround: false,
				total_devices: null,
				total_health_devices: null,
				_mapContext: null,
				selected: '',
				selectList: [{
						value: '',
						text: '全部设备',
						isDefault: false
					},
					{
						value: '0',
						text: '正常',
						isDefault: false
					},
					{
						value: '1',
						text: '水位过低',
						isDefault: false
					},
					{
						value: '2',
						text: '电量过低',
						isDefault: false
					},
					{
						value: '3',
						text: '所有故障',
						isDefault: false
					}
				],
			}
		},
		onLoad() {
			this.openLocation()
		},
		onReady() {
			this._mapContext = uni.createMapContext("map", this);
			this._mapContext.getScale({
				success(res) {
					console.log(res)
				}
			})
			this._mapContext.se
			// 仅调用初始化，才会触发 on.("markerClusterCreate", (e) => {})
			this._mapContext.initMarkerCluster({
				enableDefaultStyle: false,
				zoomOnClick: true,
				gridSize: 60,
				complete(res) {
					console.log('initMarkerCluster', res)
				}
			});

			this._mapContext.on("markerClusterCreate", (e) => {
				console.log("markerClusterCreate", e);
			});
		},
		methods: {
			navigatorPage() {
				uni.navigateTo({
					url: '/pages/maintenance/maintenance'
				})
			},
			navigatorMap() {
				let mapData = this.selectGround
				window.location.href =
					`https://apis.map.qq.com/tools/routeplan/eword=${mapData.name}&epointx=${mapData.longitude}&epointy=${mapData.latitude}?referer=myapp&key=34GBZ-ZG66D-KV54R-P3DBA-L5F6O-QQFOT`
			},
			makePhoneCall(phone) {
				uni.makePhoneCall({
					phoneNumber: phone
				})
			},
			getGroundMap(type) {
				api.getGroundMap({
					lat: this.myLatitude,
					lng: this.myLongitude,
					keywords: this.keywords,
					yj_status: this.selected
				}).then(res => {
					if (res.code == 1) {
						this.total_devices = res.data.total_devices
						this.total_health_devices = res.data.total_health_devices

						let groundMapList = res.data.list
						this.groundMapList = groundMapList.map(item => {
							return {
								id: item.id,
								latitude: item.lat,
								longitude: item.lng,
								name: item.name,
								distance: item.distance,
								city: item.city,
								address: item.address,
								kefu: item.child_mobile,
								health_devices: item.health_devices,
								total_devices: item.total_devices,
								iconPath: this.iconImage(item.yj_status),
								width: 60,
								height: 60,
								joinCluster: false, // 指定了该参数才会参与聚合

							}
						})

						this.addMarkers(type)
					}
				})
			},
			iconImage(yj_status){
				if(yj_status=='0'){
					return '../../static/images/maintenance/normal.png'
				}else if(yj_status=='1'){
					return '../../static/images/maintenance/water.png'
				}else if(yj_status=='2'){
					return '../../static/images/maintenance/electricity.png'
				}else if(yj_status=='3'){
					return '../../static/images/maintenance/fault.png'
				}else{
					return '../../static/images/logo.png'
				}
			},
			onChange(event) {
				this.getGroundMap('move')
			},
			getDetail(e) {
				console.log(e)
				let index = this.groundMapList.findIndex(item => item.id == e.detail.markerId)
				this.selectGround = this.groundMapList[index]
				this.showGround = true
			},
			myLocation() {
				this._mapContext.moveToLocation({
					latitude: this.myLatitude,
					longitude: this.myLongitude
				})
			},
			addMarkers(type) {
				this._mapContext.addMarkers({
					markers: this.groundMapList,
					clear: true,
					success() {
						console.log('addMarkers成功')
					},
					complete(res) {
						console.log('addMarkers', res)
					}
				})
				if (type == 'move' && this.groundMapList.length > 0) {
					this._mapContext.moveToLocation({
						latitude: this.groundMapList[0].latitude,
						longitude: this.groundMapList[0].longitude
					})
				}
			},
			// 使用应用内置地图查看位置。
			openLocation() {
				let that = this
				uni.getLocation({
					type: 'gcj02', //返回可以用于uni.openLocation的经纬度
					success: function(res) {
						that.myLatitude = res.latitude
						that.myLongitude = res.longitude
						that.myLocation()
						that.getGroundMap()

					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 100%;
	}

	.content {
		height: 100%;
		width: 100%;
		position: absolute;

	}

	.ground-info {
		position: absolute;
		bottom: 20px;
		z-index: 100;
		padding: 20rpx;
		width: 95%;
		left: 50%;
		transform: translateX(-50%);
		box-sizing: border-box;
		background: #ffffff;
		border-radius: 10rpx;

		.image {
			width: 32rpx;
			height: 32rpx;
			margin-right: 10rpx;
		}

		.item-top {
			display: flex;
			justify-content: space-between;

			.ground-name {
				font-weight: bold;
				font-size: 30rpx;
			}

			.distance {
				display: flex;
				align-items: center;
			}
		}

		.item-center {
			margin: 10rpx 0;
			color: #959595;
		}

		.item-bottom {
			display: flex;
			justify-content: space-between;

			.blue {
				color: #5799ec;
			}

			.phone {
				display: flex;
				align-items: center;
			}

			.device {
				font-size: 24rpx;
				display: flex;
				align-items: center;
			}
		}
	}

	.ground-list {
		position: absolute;
		bottom: 160px;
		z-index: 100;
		padding: 20rpx;
		right: 20rpx;
		background: #ffffff;
		border-radius: 10rpx;
		font-size: 22rpx;
		color: #7D8592;
		text-align: center;

		.image {
			width: 44rpx;
			height: 44rpx;
		}
	}

	.top-box {
		position: absolute;
		top: 20px;
		z-index: 100;
		padding: 20rpx;
		width: 95%;
		left: 50%;
		transform: translateX(-50%);
		box-sizing: border-box;
		background: #ffffff;
		border-radius: 10rpx;

		.item-bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 20rpx;

			.item-info {
				display: flex;
				align-items: center;
			}

			.value {
				color: #5799ec;
			}

			.image {
				width: 19rpx;
				height: 28rpx;
				margin-right: 10rpx;
			}
		}

		.item-top {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.input-search {
				position: relative;
				flex: 2;

				uni-input {
					background: #f8f8f8;
					box-shadow: 0rpx 12rpx 116rpx 0rpx rgba(196, 203, 214, 0.1);
					border-radius: 10rpx;
					font-weight: 400;
					font-size: 28rpx;
					color: #7d8592;
					padding: 14rpx 40rpx;
					font-style: normal;
				}

				uni-image {
					width: 48rpx;
					height: 48rpx;
					position: absolute;
					right: 24rpx;
					top: 14rpx;
				}
			}

			.select-box {
				flex: 1;
				margin-left: 10rpx;
				background-color: #f8f8f8;
			}
		}

	}

	.content .map {
		height: 100%;
		width: 100%;
	}
</style>